<script>
export default {
  created () {
  	var userToken=wx.getStorageSync('userToken')    //获取用户的token值，用户判断用户是否登陆过小程序
    var _this=this;
      wx.login({
        success : res =>{
           let code=res.code;
           _this.$store.dispatch('setUserCode',code);   //把获取到的code存放在vuex中进行管理
           wx.getSetting({
            success : res =>{
              if(res.authSetting['scope.userInfo']){   //判断用户是否已经授权
                wx.getUserInfo({
                  success : res =>{
                  _this.$store.dispatch('setUserInfo',res);   //把获取到的用户的信息存放在vuex中进行管理
                  if(!userToken)   //如果用户没有登陆过这个小程序
                   {
                    _this.$request.getData('https://www.rzkeji.com/api/auth/wxLogin',{raw_data:res.rawData,signature:res.signature,code:code}).then(res =>{
                      if(res.data.level=="success")
                      {
                        wx.setStorageSync('userToken',res.data.data);
                      }else{
                        wx.showToast({
                          title:res.data.message,
                          icon:'none',
                          duration:2500
                        })
                      }
                    }) 
                   }
                  }
                })   //getUserInfo结尾
              }else{
                wx.navigateTo({
                  url:"/pages/login/main"
                })
              }
            }
           })     //getSetting结尾
         }
       })    //login结尾





    // 调用API从本地缓存中获取数据
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    console.log('app created and cache logs by setStorageSync')
  }
}
</script>

<style>
page{width: 100%;height: 100%;}
.clearfix:after{content:"";height:0;line-height:0;display:block;visibility:hidden;clear:both}
.clearfix{zoom:1;}
</style>
